<template>
  <div :class="className"
       :style="{height:height,width:width}" />
</template>

<script>
import * as echarts from "echarts"

export default {
  props: {
    className: {
      type: String,
      default: "chart"
    },
    headTitle: {
      type: String,
      default: "chart"
    },
    width: {
      type: String,
      default: "100%"
    },
    height: {
      type: String,
      default: "190px"
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    xData: {
      type: Array,
      required: true
    },
    yData: {
      type: Array,
      required: true
    },
    unit: {
      type: String,
      default: ""
    },
    color: {
      type: String,
      default: "#239DD2"
    }
  },
  data() {
    return {
      chart: null
    };
  },
  watch: {
    yData: {
      deep: true,
      handler(val) {
        this.setOptions(
        );
      }
    }
  },
  mounted() {
    window.addEventListener("resize", this.resizeHandler);
    this.$nextTick(() => {
      this.initChart();
    });
  },
  beforeDestroy() {
    window.removeEventListener("resize", this.resizeHandler);
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    resizeHandler() {
      this.chart.resize();
    },
    initChart() {
      this.chart = echarts.init(this.$el);
      this.setOptions(
      );
    },
    setOptions() {
      this.chart.setOption({
        grid: {
          left: 0,
          right: 0,
          bottom: 20,
          top: 35,
          containLabel: true
        },
        xAxis: {
          data: this.xData,
          type: "category",
          axisLabel: {
            color: "#636E78",
            fontWeight: 700
          },
          axisTick: {
            show: false
          },
          axisLine: {
            show: false
          },
        },
        yAxis: {
          show: false,
          axisLine: {
            show: false
          },
        },
        series: [
          {
            //name: title,
            type: "bar",
            data: this.yData,
            animationDuration: 2800,
            animationEasing: "cubicInOut",
            showBackground: true,
            backgroundStyle: {
              color: "#EFF8FF"
            },
            itemStyle: {
              barBorderRadius: 4,
              color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                { offset: 0, color: '#1D99EF' },
                { offset: 1, color: '#1DC9EF' }
              ])
            },
            // emphasis: {
            //   itemStyle: {
            //     color: "#22AC38"
            //   },
            // },
            label: {
              //文本标签中数值的小数点精度
              show: true,
              color: "#6b6a6a",
              fontWeight: 'blod',
              distance: 0,
              verticalAlign: 'bottom',
              position: 'bottom'
            }
          },
        ]
      });

    }
  }
};
</script>
